using UnityEngine;

/// <summary>
/// 玩家作为特殊点的控制器
/// </summary>
public class PlayerSpawnPoint : MonoBehaviour
{
    [Header("鸟群设置")]
    [Tooltip("环绕半径"), Range(1f, 10f)]
    public float orbitRadius = 3f;
    [Tooltip("环绕速度"), Range(0.1f, 5f)]
    public float orbitSpeed = 2f;
    [Tooltip("吸引力强度"), Range(0.1f, 10f)]
    public float attractionStrength = 4f;
    [Tooltip("最大跟随距离"), Range(5f, 50f)]
    public float maxDistance = 20f;

    [Header("生成设置")]
    [Tooltip("初始鸟数量"), Range(0, 100)]
    public int initialBoidCount = 5;
    [Tooltip("鸟预制体")]
    public GameObject boidPrefab;
    [Tooltip("生成半径"), Range(0.1f, 10f)]
    public float spawnRadius = 2f;

    private void Start()
    {
        // 玩家初始可以有一些跟随的鸟
        GenerateInitialBoids();
    }

    /// <summary>
    /// 生成初始鸟群
    /// </summary>
    private void GenerateInitialBoids()
    {
        for (int i = 0; i < initialBoidCount; i++)
        {
            AddNewBoid();
        }
    }

    /// <summary>
    /// 添加一个新鸟到玩家跟随队列
    /// </summary>
    public void AddNewBoid()
    {
        Vector3 spawnPos = transform.position +
            Random.insideUnitSphere * spawnRadius;

        GameObject boidObj = Instantiate(boidPrefab, spawnPos, Quaternion.identity);
        Boid boid = boidObj.GetComponent<Boid>();

        boid.Initialize(
            target: transform,
            orbitRadius: orbitRadius,
            orbitSpeed: orbitSpeed,
            targetWeight: attractionStrength,
            maxDistance: maxDistance,
            targetType: Boid.TargetType.Player
        );
    }

    /// <summary>
    /// 从其他特殊点接收转移的鸟
    /// </summary>
    public void AcquireBoid(Boid boid)
    {
        boid.FollowNewTarget(transform, Boid.TargetType.Player);
    }

    /// <summary>
    /// 在编辑器中可视化范围
    /// </summary>
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, orbitRadius);
        Gizmos.color = new Color(0, 1, 0, 0.2f);
        Gizmos.DrawWireSphere(transform.position, maxDistance);
    }
}